#include "Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h" // Device header
#include "stm32f4xx_conf.h"
#include "atk_ms901m_uart.h"

static struct {
    uint8_t buf[ATK_MS901M_UART_RX_FIFO_BUF_SIZE];
    uint16_t size;
    uint16_t reader;
    uint16_t writer;
} g_uart_rx_fifo;

/**
 * @brief       ATK-MS901M UART接收FIFO写入数据
 * @param       dat: 待写入数据
 *              len: 待写入数据的长度
 * @retval      0: 函数执行成功
 *              1: FIFO剩余空间不足
 */
uint8_t atk_ms901m_uart_rx_fifo_write(uint8_t *dat, uint16_t len)
{
    for(uint16_t i=0; i<len; i++)
    {
        g_uart_rx_fifo.buf[g_uart_rx_fifo.writer] = dat[i];
        g_uart_rx_fifo.writer = (g_uart_rx_fifo.writer + 1) % g_uart_rx_fifo.size;
    }
    return 0;
}

/**
 * @brief       ATK-MS901M UART接收FIFO读取数据
 * @param       dat: 读取数据存放位置
 *              len: 欲读取数据的长度
 * @retval      0: FIFO中无数据
 *              其他值: 实际读取的数据长度
 */
uint16_t atk_ms901m_uart_rx_fifo_read(uint8_t *dat, uint16_t len)
{
    uint16_t fifo_usage;
    if(g_uart_rx_fifo.writer >= g_uart_rx_fifo.reader)
        fifo_usage = g_uart_rx_fifo.writer - g_uart_rx_fifo.reader;
    else
        fifo_usage = g_uart_rx_fifo.size - g_uart_rx_fifo.reader + g_uart_rx_fifo.writer;

    len = (len > fifo_usage) ? fifo_usage : len;
    for(uint16_t i=0; i<len; i++)
    {
        dat[i] = g_uart_rx_fifo.buf[g_uart_rx_fifo.reader];
        g_uart_rx_fifo.reader = (g_uart_rx_fifo.reader + 1) % g_uart_rx_fifo.size;
    }
    return len;
}

/**
 * @brief       ATK-MS901M UART接收FIFO清空
 * @param       无
 * @retval      无
 */
void atk_ms901m_rx_fifo_flush(void)
{
    g_uart_rx_fifo.writer = g_uart_rx_fifo.reader;
}

/**
 * @brief       ATK-MS901M UART发送数据
 * @param       dat: 待发送的数据
 *              len: 待发送数据的长度
 * @retval      无
 */
void atk_ms901m_uart_send(uint8_t *dat, uint8_t len)
{
    for(uint8_t i=0; i<len; i++)
    {
        USART_SendData(ATK_MS901M_UART, dat[i]);
        while(USART_GetFlagStatus(ATK_MS901M_UART, USART_FLAG_TXE) == RESET);
    }
}

/**
 * @brief       ATK-MS901M UART初始化
 * @param       baudrate: UART通讯波特率
 * @retval      无
 */
void atk_ms901m_uart_init(uint32_t baudrate)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef USART_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;

    /* 时钟使能 */
    ATK_MS901M_UART_TX_GPIO_CLK_ENABLE();
    ATK_MS901M_UART_RX_GPIO_CLK_ENABLE();
    ATK_MS901M_UART_CLK_ENABLE();

    /* 配置TX引脚 */
    GPIO_InitStruct.GPIO_Pin = ATK_MS901M_UART_TX_PIN;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(ATK_MS901M_UART_TX_PORT, &GPIO_InitStruct);

    /* 配置RX引脚 */
    GPIO_InitStruct.GPIO_Pin = ATK_MS901M_UART_RX_PIN;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_Init(ATK_MS901M_UART_RX_PORT, &GPIO_InitStruct);

    /* 引脚复用配置 */
    GPIO_PinAFConfig(ATK_MS901M_UART_TX_PORT, ATK_MS901M_UART_TX_PIN_SOURCE, ATK_MS901M_UART_TX_AF);
    GPIO_PinAFConfig(ATK_MS901M_UART_RX_PORT, ATK_MS901M_UART_RX_PIN_SOURCE, ATK_MS901M_UART_RX_AF);

    /* USART参数配置 */
    USART_InitStruct.USART_BaudRate = baudrate;
    USART_InitStruct.USART_WordLength = USART_WordLength_8b;
    USART_InitStruct.USART_StopBits = USART_StopBits_1;
    USART_InitStruct.USART_Parity = USART_Parity_No;
    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(ATK_MS901M_UART, &USART_InitStruct);

    /* 使能接收中断 */
    USART_ITConfig(ATK_MS901M_UART, USART_IT_RXNE, ENABLE);
    USART_Cmd(ATK_MS901M_UART, ENABLE);

    /* 配置NVIC */
    NVIC_InitStruct.NVIC_IRQChannel = ATK_MS901M_UART_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    /* 初始化FIFO */
    g_uart_rx_fifo.size = ATK_MS901M_UART_RX_FIFO_BUF_SIZE;
    g_uart_rx_fifo.reader = 0;
    g_uart_rx_fifo.writer = 0;
}

/**
* @brief       ATK-MS901M UART中断服务函数
 * @param       无
 * @retval      无
 */
void ATK_MS901M_UART_IRQHandler(void)
{
    if(USART_GetITStatus(ATK_MS901M_UART, USART_IT_ORE) == SET)
    {
        USART_ClearITPendingBit(ATK_MS901M_UART, USART_IT_ORE);
        volatile uint16_t temp = USART_ReceiveData(ATK_MS901M_UART); // 清除ORE标志
    }
    
    if(USART_GetITStatus(ATK_MS901M_UART, USART_IT_RXNE) == SET)
    {
        uint8_t tmp = USART_ReceiveData(ATK_MS901M_UART);
        atk_ms901m_uart_rx_fifo_write(&tmp, 1);
        USART_ClearITPendingBit(ATK_MS901M_UART, USART_IT_RXNE);
    }
}
